package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import entity.Asignaturas;

import BD.ConnectionFactory;

public class AsignaturasDAO {
	Connection connection = null;
	PreparedStatement ptmt = null;
	ResultSet resultSet = null;
	
	public AsignaturasDAO() {
		
	}
	
	private Connection getConnection() throws SQLException {
		Connection con;
		con = ConnectionFactory.getInstance().getConnection();
		return con;
	}
	
	public void add(Asignaturas asignatura){
		String queryString ="INSERT INTO asignaturas(id, nombre, descripcion) VALUES (?, ?, ?)";
		try {
			connection = getConnection();
			ptmt = connection.prepareStatement(queryString);
			ptmt.setInt(1, asignatura.getId());
			ptmt.setString(2, asignatura.getNombre());
			ptmt.setString(3, asignatura.getDescripcion());
			ptmt.executeUpdate();
			System.out.println("Filas anyadidas con exito");
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
			if(ptmt != null)
				ptmt.close();
			if (connection != null)
				connection.close();
			} catch (SQLException e) {
					e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
		
	}
	
	public void update(Asignaturas asignatura) {
		String queryString = "UPDATE asignaturas SET descripcion=? WHERE id=?";
		try {
			connection = getConnection();
			ptmt = connection.prepareStatement(queryString);
			ptmt.setString(1, asignatura.getDescripcion());
			ptmt.setInt(2, asignatura.getId());
			ptmt.executeUpdate();
			System.out.println("Filas modificadas con exito");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			try {
			if(ptmt != null)
				ptmt.close();
			if (connection != null)
				connection.close();
			} catch (SQLException e) {
					e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void delete(Asignaturas asignatura){
		String queryString = "DELETE FROM asignaturas WHERE id=?";
		try {
			connection = getConnection();
			ptmt = connection.prepareStatement(queryString);
			ptmt.setInt(1, asignatura.getId());
			ptmt.executeUpdate();
			System.out.println("Filas eliminadas con exito");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(ptmt != null)
					ptmt.close();
				if (connection != null)
					connection.close();
				} catch (SQLException e) {
						e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			
		}
	}
	
	public void findAll() {
		String queryString = "SELECT * FROM asignaturas";
		try {
			connection = getConnection();
			ptmt = connection.prepareStatement(queryString);
			resultSet = ptmt.executeQuery();
			while(resultSet.next()) {
				System.out.println("ID " +resultSet.getInt("id")+", Nombre "+ resultSet.getString("nombre")+", Descricpion "+ resultSet.getString("descripcion"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			try {
				if(ptmt != null)
					ptmt.close();
				if (connection != null)
					connection.close();
				} catch (SQLException e) {
						e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			
		}
	}
}
